//
//  CZQMyMainSharView.m
//  StylisticCircle
//
//  Created by zhiqiang chen on 2018/6/14.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "CZQMyMainSharView.h"
@implementation CZQMyMainSharcell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = CSClearColor;
        
        /**
         图片链接
         */
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareBtn setImage:@"icon_alipay_c_down"];
        shareBtn.clipsToBounds = YES;
        shareBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        [self addSubview:shareBtn];
        self.shareBtn = shareBtn;
        
        UILabel *shareLable = [UILabel new];
        shareLable.text = @"支付宝";
        shareLable.textColor = CSColorFromRGB(0x333333);
        shareLable.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:10];
        self.shareLable = shareLable;
        [self addSubview:shareLable];
        
        [self setConstraints];
        
    }
    return self;
}

-(void)setConstraints
{
    
    [self.shareBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self).offset(-15);
        make.centerX.mas_equalTo(self);
        make.left.mas_greaterThanOrEqualTo(self).offset(10);
        make.right.mas_lessThanOrEqualTo(self).offset(-10);
        
    }];
    
    [self.shareLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.shareBtn.mas_bottom).offset(10);
        
        make.centerX.mas_equalTo(self);
        
    }];
}

-(void)layoutSubviews
{
}

@end

@implementation CZQMyMainSharSeletView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = CSClearColor;
        
        
        /**
         内容
         */
        UILabel *titleLable = [UILabel new];
        titleLable.textColor = CSColorFromRGB(0x666666);;
        titleLable.text = @"图片形式(点击预览)";
        titleLable.font = SmallFont;
        [self addSubview:titleLable];
        self.titleLable = titleLable;
        
        /**
         图片链接
         */
        UIButton *contenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [contenBtn setImage:PlaceHold forState:UIControlStateNormal];
        contenBtn.clipsToBounds = YES;
        contenBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:contenBtn];
        self.contenBtn = contenBtn;
        
        /**
         选择
         */
        UIButton *seletnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [seletnBtn setImage:@"icon_gray_c_down" ];
        [seletnBtn setImage:[UIImage imageNamed:@"icon_yellow_c_down"] forState:UIControlStateSelected];
        seletnBtn.clipsToBounds = YES;
        seletnBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:seletnBtn];
        self.seletnBtn = seletnBtn;
        
        [self setConstraints];
        
    }
    return self;
}

-(void)setConstraints
{
   
    
    /**
     内容
     */
    [self.titleLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(10);
        make.centerX.mas_equalTo(self);
        
    }];

    
    /**
     图片链接
     */
    [self.contenBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_greaterThanOrEqualTo(self.titleLable.mas_bottom).offset(15);
        make.center.mas_equalTo(self);
        make.left.mas_greaterThanOrEqualTo(self);
        make.right.mas_lessThanOrEqualTo(self);
        
    }];
    
    /**
     选择
     */
    
    [self.seletnBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_greaterThanOrEqualTo(self.contenBtn.mas_bottom).offset(0);
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(-0);
        make.width.height.mas_equalTo(40);
    }];
    
    
}


@end

@interface CZQMyMainSharView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,weak)UICollectionView *collectionView;

@end

@implementation CZQMyMainSharView

- (instancetype)initWithFrame:(CGRect)frame
{
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.7];
        [self addSubview:alphaView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenWithAnimation)];
        [alphaView addGestureRecognizer:tap];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.layer.cornerRadius = 10;
        whiteView.backgroundColor = [CSWhiteColor colorWithAlphaComponent:1];
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"";
        titleLabel.font = MidFont;
        titleLabel.textColor = CSColorFromRGB(0x666666);
        [whiteView addSubview:titleLabel];
        self.titleLable = titleLabel;
        
        UIView * seletView = [UIView new];
        seletView.backgroundColor = [CSColorFromRGB(0xE0E0E0) colorWithAlphaComponent:0.3];
        self.seletView = seletView;
        [self.whiteView addSubview:seletView];
        
        /**
         图片
         */
        CZQMyMainSharSeletView *photoView = [CZQMyMainSharSeletView new];
        photoView.layer.borderWidth = 1;
        photoView.layer.borderColor = CSColorFromRGB(0xE0E0E0).CGColor;
        photoView.titleLable.text = @"图片形式(点击预览)";
        photoView.seletnBtn.selected = YES;
        [photoView.seletnBtn addTarget:self action:@selector(selet:)];
        photoView.seletnBtn.tag = 10;
        [self.whiteView addSubview:photoView];
        self.photoView = photoView;
        
        /**
         链接
         */
        CZQMyMainSharSeletView *urlView = [CZQMyMainSharSeletView new];
        urlView.layer.borderWidth = 1;
        urlView.layer.borderColor = CSColorFromRGB(0xE0E0E0).CGColor;
        urlView.titleLable.text = @"链接形式";
        [urlView.contenBtn setImage:@""];
        urlView.contenBtn.backgroundColor = CSColorFromRGB(0xEDEDED);
        [self.whiteView addSubview:urlView];
        self.urlView = urlView;
        [urlView.seletnBtn addTarget:self action:@selector(selet:)];
        urlView.seletnBtn.tag = 11;
        
        UIImageView *urlImage = [[UIImageView alloc]initWithImage:PlaceHold];
        [urlView.contenBtn addSubview:urlImage];
        urlImage.clipsToBounds = YES;
        urlImage.contentMode = UIViewContentModeScaleAspectFill;
        self.urlImage = urlImage;
        
        UILabel *urlLable = [ UILabel new];
        urlLable.text = @"我是陈志强爱运动，我在云咖圈等你" ;
        urlLable.numberOfLines = 0;
        urlLable.font = SmallFont;
        urlLable.textColor = CSColorFromRGB(0x666666);
        [urlView.contenBtn addSubview:urlLable];
        self.urlLable = urlLable;
        
        

        
        /**
         分享方式
         */
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(SCREENWIDTH/4, SCREENWIDTH/4 + 20);
        self.layout = layout;
        //设置内容水平
//        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        self.shareSoursArrM = [NSMutableArray array];
        
        NSMutableDictionary *parmas1 = [NSMutableDictionary dictionary];
        [parmas1 setValue:@"微信好友" forKey:@"title"];
        [parmas1 setValue:@"微信好友" forKey:@"image"];
        [self.shareSoursArrM addObject:parmas1];
        
        NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
        [parmas setValue:@"朋友圈" forKey:@"title"];
        [parmas setValue:@"朋友圈" forKey:@"image"];
        [self.shareSoursArrM addObject:parmas];
        
        
        
        NSMutableDictionary *parmas2 = [NSMutableDictionary dictionary];
        [parmas2 setValue:@"新浪微博" forKey:@"title"];
        [parmas2 setValue:@"新浪微博" forKey:@"image"];
        [self.shareSoursArrM addObject:parmas2];
        
        NSMutableDictionary *parmas3 = [NSMutableDictionary dictionary];
        [parmas3 setValue:@"QQ好友" forKey:@"title"];
        [parmas3 setValue:@"QQ" forKey:@"image"];
        [self.shareSoursArrM addObject:parmas3];
        
        
        
        UICollectionView *shareView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        shareView.backgroundColor = CSClearColor;
        self.collectionView = shareView;
        shareView.showsHorizontalScrollIndicator = NO;
        shareView.showsVerticalScrollIndicator = NO;
        [shareView setExclusiveTouch:YES];
        
        [shareView registerClass:[CZQMyMainSharcell class] forCellWithReuseIdentifier:@"CZQMyMainSharcell"];
        
        shareView.delegate = self;
        shareView.dataSource = self;
        [self.whiteView addSubview:shareView];
        self.shareView = shareView;
        
        
        
        /**
         退出
         */
        UIButton *closeBtn = [[UIButton alloc] init];
        closeBtn.backgroundColor = CSWhiteColor;
        [closeBtn setTitle:@"取消"];
        [closeBtn setTitleColor:CSColorFromRGBA(0x04030B,0.6)];
        closeBtn.titleLabel.font = kFont15;
        [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
//        [closeBtn setImage:@"pop_icon_close"];
        closeBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, TabBarHeightIphonexSafe, 0);
        [whiteView addSubview:closeBtn];
        self.popBtn = closeBtn;
        
        /**
         分割线
         */
        UIView *sepView = [[UILabel alloc]init];
        sepView.backgroundColor = CSColorFromRGB(0xF5F5F5);
        self.sepView = sepView;
        [self addSubview:sepView];
        
        [self setConstraints];
        
    }
    return self;
}

-(void)setConstraints
{
    
    
    
    /**
     背景色
     */
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    /**
     内容view
     */
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self).offset(5);
//        make.height.mas_equalTo(SCREENHEIGHT/3*2);
        make.left.width.mas_equalTo(self);
    }];
    
    
    /**
     标题
     */
    
    [self.titleLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView).offset(10);
        make.centerX.mas_equalTo(self.whiteView);
        make.bottom.mas_equalTo(self.photoView.mas_top).offset(-10);
    }];
    
    
    
    
    /**
     图片
     */
    [self.photoView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self).offset(15);
        make.width.mas_equalTo((SCREENWIDTH-30)/2);
        make.height.mas_equalTo((SCREENWIDTH-30)/6*4);
    }];
    
    [self.seletView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.center.width.height.mas_equalTo(self.photoView);
    }];
    
    /**
     链接
     */
    [self.urlView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self.photoView.mas_right).offset(-1);
        make.width.mas_equalTo((SCREENWIDTH-30)/2);
        make.height.mas_equalTo((SCREENWIDTH-30)/6*4);
    }];
    
    [self.urlView.contenBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.urlView).offset(5);
        make.right.mas_equalTo(self.urlView).offset(-5);
        make.height.mas_equalTo(50);
    }];
    
    [self.urlImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.mas_equalTo(self.urlImage.superview).offset(5);
        make.bottom.mas_equalTo(self.urlImage.superview).offset(-5);
        make.width.mas_equalTo(self.urlImage.mas_height);
    }];
    [self.urlLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.urlImage);
        make.left.mas_equalTo(self.urlImage.mas_right).offset(5);
        make.bottom.right.mas_equalTo(self.urlImage.superview).offset(-5);
        make.top.mas_equalTo(self.urlImage);
    }];
    
    /**
     分享方式
     */
    [self.shareView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.urlView.mas_bottom).offset(10);
        make.left.width.mas_equalTo(self.whiteView);
        make.height.mas_equalTo((SCREENWIDTH/4 + 20));
    }];

    
    /**
     退出
     */
    [self.popBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.shareView.mas_bottom).offset(10);
        make.left.width.mas_equalTo(self.whiteView);
        make.bottom.mas_equalTo(self.whiteView).offset(0);
        make.height.mas_equalTo(50+TabBarHeightIphonexSafe);
    }];
    
    [self.sepView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.popBtn);
        make.left.width.mas_equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    
    
    
    
}



#pragma mark - 分享类型
-(void)selet:(UIButton *)sender
{
    for (int i = 0 ; i < 2; i ++) {
        UIButton *btn1 = [self viewWithTag:10 +i];
        btn1.selected = NO;
    }
    
    sender.selected = YES;
    
    if (sender.tag == 10) {
        
        [self setNeedsUpdateConstraints];
        [UIView animateWithDuration:0.5 animations:^{
            [self.seletView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.width.height.mas_equalTo(self.photoView);
            }];
            //告知父类控件绘制，不添加注释的这两行的代码无法生效
            [self.seletView.superview layoutIfNeeded];
        }];
        
        
        
    }else
    {
        [self setNeedsUpdateConstraints];
        [UIView animateWithDuration:0.5 animations:^{
            [self.seletView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.width.height.mas_equalTo(self.urlView);
            }];
            //告知父类控件绘制，不添加注释的这两行的代码无法生效
            [self.seletView.superview layoutIfNeeded];
        }];
        

    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.shareSoursArrM.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CZQMyMainSharcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CZQMyMainSharcell" forIndexPath:indexPath];
    
    
    
    NSMutableDictionary *dic = self.shareSoursArrM[indexPath.row];
    
    [cell.shareBtn setImage:isBlankString(dic[@"image"], @"")];
    cell.shareBtn.userInteractionEnabled = NO;
    cell.shareLable.text = isBlankString(dic[@"title"], @"");
    
    return  cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.shareClick) {
        self.shareClick(indexPath.row);
        [self hiddenWithAnimation];
    }
    
}


-(void)setShareSoursArrM:(NSMutableArray *)shareSoursArrM
{
    _shareSoursArrM = shareSoursArrM;
    
    NSInteger row = 1;
    
    if (shareSoursArrM.count%4 == 0) {
        row = shareSoursArrM.count/4;
    }else
    {
        row = shareSoursArrM.count/4 + 1;
    }
    
    [self.shareView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo((SCREENWIDTH/4 + 20)*row);
    }];
    
    [self.collectionView reloadData];
}


-(void)onlyShare
{
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(0);
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(0);
    }];
    [self.photoView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.urlView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(0);
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(0);
    }];
    [self.urlView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    [self updateConstraintsIfNeeded];
}


- (void)showWithAnimation {
    
    
    
    [self addViews];
    
    CGRect frame = self.whiteView.frame;
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    self.whiteView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        self.whiteView.y = frame.origin.y;
    }];
    
}




- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    if (self.hidenClick) {
        self.hidenClick();
    }
    
    [self removeFromSuperview];
}


@end
